package com.evil.application.pojo.dto.form.control.code;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.evil.application.pojo.dto.form.control.code.config.CascadeSelectConfigDTO;
import com.evil.application.pojo.dto.form.control.option.FormOptionRespDTO;
import com.evil.application.pojo.entity.ApplicationFormControl;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择器-表单控件实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SelectControlDTO extends ControlDTO {

    public static String[] ignoreProperties = new String[]{"cascadeSelectConfig", "options"};

    /**
     * 选项分隔符
     */
    private String selectSeparator;

    /**
     * 允许半选(0:否, 1:是)
     */
    private Integer halfSelected;

    /**
     * 是否开启其他选项(0:否, 1:是)
     */
    private Integer otherSelect;

    /**
     * 级联选择配置
     */
    private CascadeSelectConfigDTO cascadeSelectConfig;

    /**
     * 选项树
     */
    private List<FormOptionRespDTO> options = new ArrayList<>();

    /**
     * 选项列表
     */
    @JSONField(serialize = false)
    private List<FormOptionRespDTO> optionsData = new ArrayList<>();

    /**
     * 是否可以多选(0:否, 1:是)
     */
    private Integer multiple;

    /**
     * 是否可以筛选选项(0:否, 1:是)
     */
    private Integer filterable;

    /**
     * 选项样式(默认: default, 按钮: button)
     */
    private String optionStyle;

    /**
     * 最少选择
     */
    private Integer minSelected;

    /**
     * 最大选择
     */
    private Integer maxSelected;

    public SelectControlDTO(ApplicationFormControl applicationFormControl) {
        super(applicationFormControl, ignoreProperties);

        this.selectSeparator = applicationFormControl.getSelectSeparator();
        if (StringUtils.isNotBlank(applicationFormControl.getCascadeSelectConfig())) {
            this.cascadeSelectConfig = JSON.parseObject(applicationFormControl.getCascadeSelectConfig(), CascadeSelectConfigDTO.class);
        }
    }
}
